<template>
  <div class="image-display">
    <div class="image-wrapper">
      <div v-if="loading" class="loading-overlay">
        <div class="loader"></div>
      </div>
      <div v-if="error" class="error-overlay">图片加载失败</div>
      <img
        v-if="imageUrl"
        :src="imageUrl"
        :alt="alt"
        @load="handleImageLoad"
        @error="handleImageError"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const props = defineProps<{
  imageUrl: string
  alt?: string
}>()

const loading = ref(true)
const error = ref(false)

const handleImageLoad = () => {
  loading.value = false
}

const handleImageError = () => {
  loading.value = false
  error.value = true
}
</script>

<style scoped>
.image-display {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: 1rem;
}

.image-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: 8px;
  overflow: hidden;
  background: #f5f5f5;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.loading-overlay,
.error-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.9);
}

.loader {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.error-overlay {
  color: #ff4d4f;
  font-size: 1rem;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
